package com.hotchpotch.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hotchpotch.R;
import com.hotchpotch.adapter.DBFlowAdapter;
import com.hotchpotch.adapter.DBFlowAdapter2;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.base.BaseRecyclerAdapter;
import com.hotchpotch.bean.DBFlowTestBean;
import com.hotchpotch.bean.DBFlowTestBean_Table;
import com.hotchpotch.inter.OnRecyclerViewItemClickListener;
import com.hotchpotch.util.Logg;
import com.hotchpotch.util.ToastUtil;
import com.hotchpotch.widget.SpaceItemDecoration;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.ModelAdapter;

import java.util.List;
import java.util.Random;

import butterknife.BindView;

public class DBFlowAct extends BaseActivity {
    private static final String TAG = "DBFlowAct";
    @BindView(R.id.act_dbflow_et)
    EditText editText;
    @BindView(R.id.act_dbflow_btn1)
    Button btn1;
    @BindView(R.id.act_dbflow_btn2)
    Button btn2;
    @BindView(R.id.act_dbflow_btn3)
    Button btn3;
    @BindView(R.id.act_dbflow_btn4)
    Button btn4;
    @BindView(R.id.act_dbflow_btn5)
    Button btn5;
    @BindView(R.id.act_dbflow_btn6)
    Button btn6;
    @BindView(R.id.act_dbflow_btn7)
    Button btn7;
    @BindView(R.id.act_dbflow_rec)
    RecyclerView recyclerView;
    @BindView(R.id.act_dbflow_tv)
    TextView textView;

    //    private DBFlowAdapter adapter;
    private DBFlowAdapter2 adapter2;

    private List<DBFlowTestBean> list;
    private int insertCount = 0;
    private ModelAdapter<DBFlowTestBean> modelAdapter = FlowManager.getModelAdapter(DBFlowTestBean.class);

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    textView.setText("当前数据条数 = " + list.size());
//                    adapter.setList(list);
                    adapter2.setData(list);
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.act_dbflow;
    }

    @Override
    protected void initView() {
//        adapter = new DBFlowAdapter(this);
        adapter2 = new DBFlowAdapter2(this, R.layout.item_dbflow);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new SpaceItemDecoration(0, 0, 0, 10));
//        recyclerView.setAdapter(adapter);
        recyclerView.setAdapter(adapter2);
    }

    @Override
    protected void initData() {
        if (list == null) {
            textView.setText("当前数据条数 = 0");
        }
    }

    @Override
    protected void initEvents() {
        btn1.setOnClickListener(clickListener);
        btn2.setOnClickListener(clickListener);
        btn3.setOnClickListener(clickListener);
        btn4.setOnClickListener(clickListener);
        btn5.setOnClickListener(clickListener);
        btn6.setOnClickListener(clickListener);
        btn7.setOnClickListener(clickListener);
        //现在的点击事件
        adapter2.setOnItemClick(new BaseRecyclerAdapter.OnItemClick() {
            @Override
            public void onClick(View v, int position) {
                ToastUtil.showToast("点击项id = " + list.get(position).getId());
            }

            @Override
            public void onLongClick(View v, int position) {

            }
        });
    }

    /**
     * 存储数据
     */
    private void saveData() {
        DBFlowTestBean bean = new DBFlowTestBean();
        bean.setName("撒旦");
        bean.setAge((new Random().nextInt(100)) + "");
        bean.setSex("男");
//        bean.insert();
        bean.save();
//        adapter.insert(bean);
        insertCount++;
        ToastUtil.showToast("插入成功" + insertCount + "次");
        showData();
    }

    /**
     * 删除所有
     */
    private void deleteData() {
        if (list != null) {
            list.clear();
        }
        if (TextUtils.equals(editText.getText().toString().trim(), "")) {
            ToastUtil.showToast("请输入要删除的年龄");
            return;
        }
        list = SQLite.select()
                .from(DBFlowTestBean.class)
                .where(DBFlowTestBean_Table.age.eq(editText.getText().toString().trim()))
                .queryList();
        modelAdapter.deleteAll(list);
        /*SQLite.delete()
                .from(DBFlowTestBean.class)
                .where(DBFlowTestBean_Table.age.eq(editText.getText().toString().trim()))
                .limit(3)
                .execute();*/
        showData();
    }

    /**
     * 修改指定数据
     */
    private void updateData() {
        if (list != null) {
            list.clear();
        }
        if (TextUtils.equals(editText.getText().toString().trim(), "")) {
            ToastUtil.showToast("请输入要修改的年龄");
            return;
        }
        list = SQLite.select()
                .from(DBFlowTestBean.class)
                .where(DBFlowTestBean_Table.age.eq(editText.getText().toString().trim()))
                .queryList();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSex("女");
            list.get(i).setName("科雷娜拉" + i);
        }
        modelAdapter.saveAll(list);
        ToastUtil.showToast("修改成功" + list.size() + "条数据");
        showData();
    }

    /**
     * 修改大于多少岁的数据
     */
    private void update2Data() {
        if (list != null) {
            list.clear();
        }
        if (TextUtils.equals(editText.getText().toString().trim(), "")) {
            ToastUtil.showToast("请输入要修改数据的年龄");
            return;
        }
        list = SQLite.select()
                .from(DBFlowTestBean.class)
                .where(DBFlowTestBean_Table.age.greaterThan(editText.getText().toString().trim()))
                .queryList();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSex("女");
            list.get(i).setName("科雷娜拉");
        }
        modelAdapter.saveAll(list);
        ToastUtil.showToast("修改成功" + list.size() + "条数据");
        showData();
    }

    private List<DBFlowTestBean> queryData() {
        if (list != null) {
            list.clear();
        }
        list = SQLite.select()
                .from(DBFlowTestBean.class)
                .queryList();
        if (list.size() == 0) {
            ToastUtil.showToast("没有数据!");
        }
        return list;
    }

    /**
     * 查询所有数据并显示
     */
    private void showData() {
        if (list != null) {
            list.clear();
        }
        list = SQLite.select()
                .from(DBFlowTestBean.class)
//                .where(DBFlowTestBean_Table.id.greaterThan(0l))
                .queryList();
        if (list != null) {
//            Logg.e(TAG, "查询的结果 = " + list);
            handler.sendEmptyMessage(0);
        }
    }

    /**
     * 查询第一条
     */
    private void querySingle() {
        if (list != null) {
            list.clear();
        }
        DBFlowTestBean dbFlowTestBean = SQLite.select()
                .from(DBFlowTestBean.class)
                .querySingle();
        list.add(dbFlowTestBean);
        handler.sendEmptyMessage(0);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.act_dbflow_btn1://存数据
                    saveData();
                    break;
                case R.id.act_dbflow_btn2://删除数据，第一条开始
                    deleteData();
                    break;
                case R.id.act_dbflow_btn3://显示数据
                    showData();
                    break;
                case R.id.act_dbflow_btn4://修改数据
                    updateData();
                    break;
                case R.id.act_dbflow_btn5://删除所有数据
                    modelAdapter.deleteAll(queryData());
                    showData();
                    break;
                case R.id.act_dbflow_btn6://大于多少岁修改数据
                    update2Data();
                    break;
                case R.id.act_dbflow_btn7://查询第一条
                    querySingle();
                    break;
            }
        }
    };
}
